﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class TinhTrangSanPhamDAO: BaseDataProvider
    {
        MySqlCommand _cmd;

        public Boolean ThemTinhTrangSanPham(TinhTrangSanPhamDTO tt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into TinhTrangSanPham(TenTinhTrangSP) values(@TenTinhTrangSP)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenTinhTrangSP", tt.TenTinhTrangSP));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaTinhTrangSanPham(TinhTrangSanPhamDTO tt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from TinhTrangSanPham where MaTinhTrangSP = @MaTinhTrangSP";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangSP", tt.MaTinhTrangSP));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatBangTinhTrangSanPham(TinhTrangSanPhamDTO tt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update TinhTrangSanPham set TenTinhTrangSP = @TenTinhTrangSP where MaTinhTrangSP = @MaTinhTrangSP";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangSP", tt.MaTinhTrangSP));
            _cmd.Parameters.Add(new MySqlParameter("@TenTinhTrangSP", tt.TenTinhTrangSP));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean TinhTrangSanPham(int MaTTSP)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete TinhTrangSanPham where MaTinhTrangSP = @MaTinhTrangSP";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangSP", MaTTSP));

            return ExecuteNonQuery(_cmd);
        }

        public List<TinhTrangSanPhamDTO> LayDanhSachTinhTrangSanPham()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from TinhTrangSanPham";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<TinhTrangSanPhamDTO> list = new List<TinhTrangSanPhamDTO>();

            foreach (DataRow row in dt.Rows)
            {
                TinhTrangSanPhamDTO dto = new TinhTrangSanPhamDTO();
                dto.MaTinhTrangSP = int.Parse(row["MaTinhTrangSP"].ToString());
                dto.TenTinhTrangSP = row["TenTinhTrangSP"].ToString();

                list.Add(dto);
            }

            return list;
        }
    }
}
